import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class ExonProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Notepad++\\notepad++.exe");
         
         
        /* DateFormat hhmmFormat = new SimpleDateFormat("yyyyMMddhh:mm");
         Date date = new Date();
		String yyyMMdd = hhmmFormat.format(date).substring(0, 8); 
		String MaintenanceStart = "15:00";
		String MaintenanceEnd = "19:00";
		if(hhmmFormat.parse(yyyMMdd + MaintenanceStart).before(date) && hhmmFormat.parse(yyyMMdd + MaintenanceEnd).after(date)){
		System.out.println("HIIIII");
		}
		System.out.println(yyyMMdd);
		Calendar calender = Calendar.getInstance();
		calender.setMinimalDaysInFirstWeek(5);
		calender.setLenient(true);
		
		System.out.println(calender.getFirstDayOfWeek());
		System.out.println(calender.get(Calendar.DAY_OF_WEEK));
		*/
        int j =2;
		while(j != 0){
	         Worker worker = new Worker(p);
	         //Worker worker1 = new Worker(p);
	         
	         
	         /*thread th = new thread();
	         ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
	         executor.scheduleAtFixedRate(th, 0, 1, TimeUnit.MINUTES);*/
	          //th.start();
	         worker.start();
	         System.out.println(worker.getName()+"\t"+worker.isAlive());
	         worker.join(25000);
	         
	         System.out.println(worker.getName()+"\t"+worker.isAlive());
	         if (worker.exit != null)
			    {
		            int i = worker.exit;
			        System.out.println("Return Type : " + i);
			        break;
					
			    }
			    else {
			    	
			    	worker.interrupt();
			    	System.out.println("first time not executed"+j);
			    	System.out.println("forcefully interrupted"+j);
			    /*	System.out.println("second time executing");
			    	worker1.start();
			    	worker1.join(25000);
			    	if (worker1.exit != null)
				    {
			            int i = worker1.exit;
				        System.out.println("Return Type : " + i);
						
				    }else {
				    	worker1.interrupt();
				    	System.out.println("forcefully interrupted");
				    }*/
			    	
			    }
			j--;
		}

         // cause this process to stop until process p is terminated
        // p.waitFor();

         // when you manually close notepad.exe program will continue here
         System.out.println("Waiting over.");

      } catch (Exception ex) {
         ex.printStackTrace();
         System.out.println("exception occured");
      }
     

   }
   private static class Worker extends Thread {
		private final Process process;
		private Integer exit;
		private Worker(Process process) {
			this.process = process;
		}
		public void run() {
			try { 
				System.out.println("Enter in process");
				exit = process.waitFor();
				System.out.println("Enter out process");
				
			} catch (InterruptedException ignore) {
				ignore.printStackTrace();
				return;
			}
		}  
	}
   private static class thread extends Thread {
	   @Override
	public void run() {
		System.out.println("Hiiiiiiiiiii");
	}
   }
}